package com.csun.cmny.provider.web.frontend;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OpcMessagePushWay;
import com.csun.cmny.provider.model.dto.message.MessagePushWayQueryDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OpcMessagePushWayService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-11-07 17:11
 * @Description:
 */
@RestController
@RequestMapping(value = "/messageWay", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OpcMessagePushWayController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OpcMessagePushWayService opcMessagePushWayService;

    /**
     * 添加
     */
    @PostMapping(value = "/add")
    public Wrapper add(@RequestBody OpcMessagePushWay opcMessagePushWay) {

        Integer result = opcMessagePushWayService.add(opcMessagePushWay, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /**
     * 修改
     */
    @PostMapping(value = "/edit")
    public Wrapper edit(@RequestBody OpcMessagePushWay opcMessagePushWay) {

        Integer result = opcMessagePushWayService.edit(opcMessagePushWay, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 删除
     */
    @PostMapping(value = "/deleteById/{id}")
    public Wrapper deleteById(@PathVariable Long id) {

        Integer result = opcMessagePushWayService.deleteById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /**
     * 查询 - 分页
     */
    @PostMapping(value = "/queryListWithPage")
    public Wrapper queryListWithPage(@RequestBody MessagePushWayQueryDto messagePushWayQueryDto) {

        List<OpcMessagePushWay> list = opcMessagePushWayService.queryListWithPage(messagePushWayQueryDto);

        //return WrapMapper.ok(new PageInfo<>(list));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), new PageInfo<>(list));
    }

}
